/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.shop;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopLuckyBurstCoupon;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkShopLuckyBurstCouponDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopLuckyBurstCouponAdv;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkShopLuckyBurstCouponAdvDao;

/**
 * 幸运爆款优惠券Service
 * @author 李欣荣
 * @version 2018-10-11
 */
@Service
@Transactional(readOnly = true)
public class HkShopLuckyBurstCouponService extends CrudService<HkShopLuckyBurstCouponDao, HkShopLuckyBurstCoupon> {

	@Autowired
	private HkShopLuckyBurstCouponAdvDao hkShopLuckyBurstCouponAdvDao;
	
	public HkShopLuckyBurstCoupon get(String id) {
		HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon = super.get(id);
		hkShopLuckyBurstCoupon.setHkShopLuckyBurstCouponAdvList(hkShopLuckyBurstCouponAdvDao.findList(new HkShopLuckyBurstCouponAdv(hkShopLuckyBurstCoupon)));
		return hkShopLuckyBurstCoupon;
	}
	
	public List<HkShopLuckyBurstCoupon> findList(HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon) {
		return super.findList(hkShopLuckyBurstCoupon);
	}
	
	public Page<HkShopLuckyBurstCoupon> findPage(Page<HkShopLuckyBurstCoupon> page, HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon) {
		return super.findPage(page, hkShopLuckyBurstCoupon);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon) {
		super.save(hkShopLuckyBurstCoupon);
		for (HkShopLuckyBurstCouponAdv hkShopLuckyBurstCouponAdv : hkShopLuckyBurstCoupon.getHkShopLuckyBurstCouponAdvList()){
			if (hkShopLuckyBurstCouponAdv.getId() == null){
				continue;
			}
			if (HkShopLuckyBurstCouponAdv.DEL_FLAG_NORMAL.equals(hkShopLuckyBurstCouponAdv.getDelFlag())){
				if (StringUtils.isBlank(hkShopLuckyBurstCouponAdv.getId())){
					hkShopLuckyBurstCouponAdv.setBurst(hkShopLuckyBurstCoupon);
					hkShopLuckyBurstCouponAdv.preInsert();
					hkShopLuckyBurstCouponAdvDao.insert(hkShopLuckyBurstCouponAdv);
				}else{
					hkShopLuckyBurstCouponAdv.preUpdate();
					hkShopLuckyBurstCouponAdvDao.update(hkShopLuckyBurstCouponAdv);
				}
			}else{
				hkShopLuckyBurstCouponAdvDao.delete(hkShopLuckyBurstCouponAdv);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon) {
		super.delete(hkShopLuckyBurstCoupon);
		hkShopLuckyBurstCouponAdvDao.delete(new HkShopLuckyBurstCouponAdv(hkShopLuckyBurstCoupon));
	}
	
}